#include "stdafx.h"
#include "Thirsty.h"
#include "myRand.h"
#include "terrain.h"
#include "Forage.h"
#include "Dead.h"
#include "Ant.h"

extern int terrain[kMaxRows][kMaxCols];

CThirsty * CThirsty::instance()
{
	static CThirsty inst;
	return (&inst);
}

void CThirsty::Enter(CAnt *pAnt)
{
}

void CThirsty::Execute(CAnt *pAnt)
{
	int rowMove, colMove;
	int newRow, newCol;
	int waterRow, waterCol;
	int poisonRow, poisonCol;

	rowMove = rnd(0,2)-1;
	colMove = rnd(0,2)-1;

	newRow = pAnt->row+rowMove;
	newCol = pAnt->col + colMove;
	if(newRow < 0) return;
	if(newCol < 0) return;
	if (newRow >= kMaxRows-1) return;
	if(newCol >= kMaxCols-1) return;

	if((terrain[newRow][newCol] == kGround) ||
		(terrain[newRow][newCol] == kFood)) {
		pAnt->row = newRow;
		pAnt->col = newCol;
	}

	if(terrain[newRow][newCol] == kWater){
		pAnt->row = newRow;
		pAnt->col = newCol;
		terrain[pAnt->row][pAnt->col] = kGround;
//		state = kForage;
		do {
			waterRow = rnd(0, kMaxRows-1);
			waterCol = rnd(0, kMaxCols-1);
		}while(terrain[waterRow][waterCol] != kGround);
		terrain[waterRow][waterCol] = kWater;
		pAnt->changeState(CForage::instance());
	}

	if(terrain[newRow][newCol] == kPoison){
		pAnt->row = newRow;
		pAnt->col = newCol;
		terrain[pAnt->row][pAnt->col] = kGround;
//		state = kDead;
		do {
			poisonRow = rnd(0, kMaxRows-1);
			poisonCol = rnd(0, kMaxCols-1);
		}while(terrain[poisonRow][poisonCol] != kGround);
		terrain[poisonRow][poisonCol] = kPoison;
		pAnt->changeState(CDead::instance());
	}
}

void CThirsty::Exit(CAnt *pAnt)
{
}
